<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-form-item label="名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item label="编码" prop="code">
				<el-input v-model="dataForm.code" placeholder="编码"></el-input>
			</el-form-item>
			<el-form-item label="所属组织" prop="organizationId">
				<el-tree-select v-model="dataForm.organizationId" :data="organList" value-key="id" check-strictly
					:render-after-expand="false" :props="{ value: 'id', label: 'abbreviation', children: 'children' }"
					style="width: 100%" />
			</el-form-item>
			<el-form-item label="职位类别" prop="positionType">
				<el-tree-select v-model="dataForm.positionType" :data="positionList" value-key="id" check-strictly
					:render-after-expand="false" :props="{ value: 'id', label: 'name', children: 'children' }"
					style="width: 100%" />
			</el-form-item>
			<el-form-item label="对应职务" prop="jobId">
				<el-tree-select v-model="dataForm.jobId" :data="jobList" value-key="id" check-strictly
					:render-after-expand="false" :props="{ value: 'id', label: 'name', children: 'children' }"
					style="width: 100%" />
			</el-form-item>
			<el-form-item label="对应职级" prop="jobLevel">
				<el-tree-select v-model="dataForm.jobLevel" :data="jobLevelList" value-key="id" check-strictly
					:render-after-expand="false" :props="{ value: 'id', label: 'name', children: 'children' }"
					style="width: 100%" />
			</el-form-item>
			<el-form-item label="职位描述" prop="description">
				<el-input v-model="dataForm.description" placeholder="职位描述" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="是否启用" prop="isEnable">
				<el-switch v-model="dataForm.isEnable" :active-value="1" :inactive-value="0" active-text="启用" inline-prompt
					inactive-text="禁用" placeholder="" />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { usePositionApi, usePositionSubmitApi } from '@/api/nb/organization/position'
import { findListAll } from '@/api/nb/organization/organization'
import { findAll } from '@/api/nb/organization/position/type'
import { findByPcode } from '@/api/nb/business/dictionaries_subset'
import { findAll as findJobLevel } from '@/api/nb/organization/position/level'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',

	name: '',
	code: '',
	organizationId: '',
	positionType: '',
	jobId: '',
	jobLevel: '',
	description: '',
	isEnable: 1
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getPosition(id)
	}

	getOrgan()
	getPositionList()
	getJobList()
	getJobLevelList()
}

const getPosition = (id: number) => {
	usePositionApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})
const organList = ref({})
const positionList = ref({})
const jobList = ref({})
const jobLevelList = ref({})

// 获取 所属组织
const getOrgan = () => {
	return findListAll().then(res => {
		organList.value = res.data
	})
}

// 获取 所属职位
const getPositionList = () => {
	return findAll().then(res => {
		positionList.value = res.data
	})
}

// 获取 所属职务
const getJobList = () => {
	return findByPcode('ZhiWu').then(res => {
		jobList.value = res.data.list
	})
}

// 获取 所属职级
const getJobLevelList = () => {
	return findJobLevel().then(res => {
		jobLevelList.value = res.data
	})
}

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		usePositionSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
